﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ForkCarRelationManager.db;
using ForkCarRelationManager.DB;
using DES;
namespace ForkCarRelationManager
{
   
    public partial class FormLogin : Form
    {
        DESCoder des = new DESCoder("7557");
        dbOperate dbo = new dbOperate();
        static public string admin = "";
        public FormLogin()
        {
            InitializeComponent();
            DateTime todaytime=DateTime.Now;
            DateTime dt1 = new DateTime(2016, 8, 20);
          
            string endtimestr=dbo.GetOneData("select DBAT from power where ID=1");
            if (endtimestr == "Ox+T1xuEmipe2JC/rsFM0vF5yksPbPlzv5CR9niTKr6CTIbD13StBw==")
            {
                endtimestr = des.Decrypt(endtimestr);
                DateTime enddate = Convert.ToDateTime(endtimestr);
                if (DateTime.Now <= enddate)
                {
                    string countstr = dbo.GetOneData("select CNT from power where ID=1");
                    int count = Convert.ToInt32(des.Decrypt(countstr));
                    if (count != 0)
                    {
                        count--;
                        countstr = des.Encrypt(count.ToString());
                        dbo.UpdateAndInsert("update power set CNT ='" + countstr + "' where ID=1");
                        buttonX1.Enabled = true;
                    }
                    else
                    {
                        MessageBox.Show("访问权限已到期");
                        buttonX1.Enabled = false;
                    }
                }
                else
                {
                    MessageBox.Show("访问权限已到期");
                    buttonX1.Enabled = false;
                }
            }
            else
            {
                MessageBox.Show("访问权限已到期");
                buttonX1.Enabled = false;
            }
           /* RegistryKey mainkey = Registry.LocalMachine;
            RegistryKey subkey = mainkey.OpenSubKey("SOFTWARE\\Register", true);
            DateTime dt1 = new DateTime(2016, 8, 20);
            DateTime dt2 = new DateTime(2016, 7, 20);
            if (subkey == null)
            {
                subkey = mainkey.CreateSubKey("SOFTWARE\\Register");
                
                subkey.SetValue("使用时间", dt1);
                subkey.SetValue("起始时间时间", dt2);
            }
            string begintimestr = subkey.GetValue("起始时间时间", dt2).ToString();
            DateTime begintime = Convert.ToDateTime(begintimestr);
            DateTime time=begintime.
            string allowLogintimestr = subkey.GetValue("使用时间", dt1).ToString();
            DateTime allotime = Convert.ToDateTime(allowLogintimestr);
           // DateTime allowLogintime = (DateTime)subkey.GetValue("使用时间",dt1);
            if (DateTime.Now > allotime)
            {
                MessageBox.Show("访问权限已到期");
                buttonX1.Enabled = false;
            }
            else
                buttonX1.Enabled = true;*/
            
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            dbOperate dbo = new dbOperate();
            string user = username.Text;
            string psw = password.Text;
            if (user.Equals("") || psw.Equals(""))
            {
                MessageBox.Show("用户名或密码不正确");
            }
            else if (user.IndexOf("'") >= 0 || user.IndexOf("|") >= 0 || user.IndexOf("=") >= 0 || psw.IndexOf("'") >= 0 || psw.IndexOf("|") >= 0 || psw.IndexOf("=") >= 0)
            {
                MessageBox.Show("不要使用特殊字符");
            }
            else
            {
                //count = Convert.ToInt32(dbo.GetCount("admin", "where UserName='" + user + "' and PassWord='" + psw + "'"));
                DataTable table = dbo.GetDate("select * from admin where UserName='" + user + "' and PassWord='" + psw + "'");
                if (table != null && table.Rows.Count > 0)//用户名密码存在且正确
                {
                    //保存登录状态
                    LoginInfo.CurrentUser.Account = Convert.ToInt32(table.Rows[0]["ID"].ToString());
                    LoginInfo.CurrentUser.AccountName = user;
                    LoginInfo.CurrentUser.LoginTime = DateTime.Now;
                    MainView mainview = new MainView(table);
                    mainview.Show();
                    this.Hide();
                }
                else
                {
                    //result.Text = "用户名或密码不正确";
                    MessageBox.Show("用户名或密码不正确");
                }
            }
           
            
            
        }

        private void labelX4_Click(object sender, EventArgs e)
        {

        }

    }
}
